

$("#catagoryDiv").on('click', '.oneCategoryText', function () {
    $(this).siblings('.oneCategoryText').css('border', '0px black solid')
    $(this).siblings('.oneCategoryText').attr('isSelect', 'false');

    $(this).css('border', '3px black solid');
    $(this).attr('isSelect', 'true');

    loadTwoCategory();
});

$("#catagoryDiv").on('click', '.twoCategoryText', function () {
    var size = $(this).css('border-width');
    if(size === '0px') {
        $(this).css('border-width', '3px');
    } else {
        $(this).css('border-width', '0px');
    }
});

function addCatagory(obj, type){
    var param = {
        'c_name' : $(obj).siblings('input[name="c_name"]').val(),
        'c_desc' : $(obj).siblings('input[name="c_desc"]').val()
    };

    if(type === 'two') {
        param.c_pid = $('span[isSelect="true"]').attr('c_id');
        if(!param.c_pid) {
            alert('请选中一级分类标题')
            return;
        }
    }

    if(param['c_name'] === '') {
        return;
    }

    $.ajax({
        url :  '/category/insert',
        data : param,
        type : 'post',
        async : false,
        success : function(data) {
            if(type === 'one'){
                loadOneCategory()
            } else {
                loadTwoCategory()
            }

            //文本框重置为空
            // $(obj).siblings('input').val('');
        }
    });
}


function loadOneCategory() {
    $.ajax({
        url :  '/category/all/one',
        type : 'post',
        async : false,
        dataType: 'json',
        success : function(data) {
            if(!data.list) {
                return;
            }

            $('.oneCategoryDiv').html('');
            for (let i = 0; i < data.list.length; i++) {
                var item = data.list[i];
                $('.oneCategoryDiv').append(
                    ' <span class="oneCategoryText" c_id="'+item.c_id+'">' +
                    item.c_name + '('+item.c_id + ')'+
                    '</span>');
            }

            $('.oneCategoryDiv .oneCategoryText').eq(0).click();
        }
    });
}
loadOneCategory();

function loadTwoCategory() {
    $.ajax({
        url :  '/category/all/two?pId='+( $('span[isSelect="true"]').attr('c_id')),
        type : 'post',
        async : false,
        dataType: 'json',
        success : function(data) {
            if(!data.list) {
                return;
            }

            $('.twoCategoryDiv').html('');
            for (let i = 0; i < data.list.length; i++) {
                var item = data.list[i];
                $('.twoCategoryDiv').append(
                    ' <span class="twoCategoryText" c_id="'+item.c_id+'">' +
                    item.c_name + '('+item.c_id + ')'+
                    '</span>');
            }
        }
    });
}

function delCategory() {
    var cId = $('#delCId').val();
    var childrenCount = 0;
    var contentCount = 0;

    $.ajax({
        url :  '/category/count?cId=' + cId,
        type : 'post',
        async : false,
        dataType: 'json',
        success : function(data) {
            contentCount = data.contentCount;
            childrenCount = data.childrenCount;
        }
    });

    if(childrenCount > 0 || contentCount > 0) {
        alert("还存在引用，不能删除")
        return;
    }

    var isDel = confirm('确认删除');
    if(!isDel) {
        return;
    }

    $.ajax({
        url :  '/category/del?cId=' + cId,
        type : 'post',
        async : false,
        success : function(data) {
            alert('删除成功')
            location.reload();
        }
    });
}

function updCategory() {

    $.ajax({
        url :  '/category/update',
        data : {
            'c_id' : $('#updCId').val(),
            'c_name' : $('#updCName').val(),
            'c_desc' : $('#updCdesc').val()
        },
        type : 'post',
        async : false,
        success : function(data) {
            alert('修改成功')
            location.reload();
        }
    });
}